﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SchemaMonitor.json
{
	public class CSoftCommentList : List<CSoftComment>
	{
		public CSoftCommentList() : base() { }
		public CSoftCommentList(int count) : base(count) { }
		public CSoftCommentList(CSoftCommentList list) : base(list) { }

		public int CountRecursive { get { return CountRecursive_(3); } }
		private int CountRecursive_(int maxDepth)
		{
			int count = this.Count;
			foreach (var i in this)
				if (i.HasComments && maxDepth > 0)
					count += i.Comments.CountRecursive_(maxDepth - 1);
			return count;
		}
	}
	public class CSoftComment : CSoft
	{
		//Constructor
		public CSoftComment(CNodeBasic root, CUser cache) : base(root, cache)
		{
		}

		//read fields
		public string Message { get { return GetStr("message"); } }
		public bool HasLikes { get { return GetBool("user_likes") && Fields.Has("likes"); } }
		public int LikeCount { get { return GetInt("like_count"); } }
		public bool HasFrom { get { return Fields.Has("from"); } }
		public bool HasTags { get { return Fields.Has("tags"); } }
		public bool HasComments { get { return Fields.Has("comments"); } }

		public CNodeList Tags { get { return Fields["tags"].AsPointerList(_cache.All); } }
		public CNodeList Likes_ { get { return Fields["likes"].AsPointerList(_cache.All); } }
		public CNodeList Comments_ { get { return Fields["comments"].AsPointerList(_cache.All); } }
		public List<string> Likes
		{
			get
			{
				var c = Likes_;
				var list = new List<string>(c.Count);
				foreach (var i in c)
					list.Add(new CSoftUser(i.Root, _cache).Name);
				return list;
			}
		}

		public CSoftCommentList Comments
		{
			get
			{
				var c = Comments_;
				var list = new CSoftCommentList(c.Count);
				foreach (var i in c)
					list.Add(new CSoftComment(i.Root, _cache));
				return list;
			}
		}


		public CSoftUser From { get { return GetUser("from", _cache); } }
		public string Name
		{
			get
			{
				if (null != From)
					return From.Name;
				return GetStr("name");
			}
		}

	}
}
